package com.jrx.anytxn.param.transaction.job.billCompare.step;

import com.jrx.anytxn.number.service.ISegmentService;
import com.jrx.anytxn.param.constant.Constant;
import com.jrx.anytxn.param.entity.TlBillTransCompareDetail;
import com.jrx.anytxn.param.entity.TlBillTransDetil;
import com.jrx.anytxn.param.mapper.ext.ExtTlBillTransDetilMapper;
import com.jrx.anytxn.param.transaction.config.BillCompareConfig;
import com.jrx.anytxn.param.transaction.job.billCompare.bean.BillDetilReadBean;
import com.jrx.anytxn.transaction.entity.TlPaymentLog;
import org.springframework.batch.item.ItemProcessor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class BillDetailForHKProcessor implements ItemProcessor<TlPaymentLog, BillDetilReadBean> {

    @Resource
    private ExtTlBillTransDetilMapper extTlBillTransDetilMapper;
    @Autowired
    private ISegmentService iSegmentService;
    @Autowired
    private BillCompareConfig billCompareConfig;

    @Override
    public BillDetilReadBean process(TlPaymentLog item) throws Exception {

        List<TlBillTransDetil> tlBillTransDetilList =  new ArrayList<>();
        List<TlBillTransCompareDetail> tlBillTransCompareDetailList =  new ArrayList<>();
        TlBillTransDetil tlBillTransDetil = extTlBillTransDetilMapper.selectByLoanNumber(item.getRefNo());
        if(null != tlBillTransDetil) {
            String transStatus = billCompareConfig.getTransType().get(tlBillTransDetil.getTransStatus());
            if (!item.getSumStatus().equals(transStatus)
                    || item.getAmount().compareTo(tlBillTransDetil.getTransactionAmount()) != 0) {
                TlBillTransCompareDetail tlBillTransCompareDetail = new TlBillTransCompareDetail();
                tlBillTransCompareDetail.setId(iSegmentService.getId("TlBillTransCompareDetail"));
                tlBillTransCompareDetail.setTenantId(tlBillTransDetil.getTenantId());
                tlBillTransCompareDetail.setOrganizationId(tlBillTransDetil.getOrganizationId());
                tlBillTransCompareDetail.setBillType(Constant.TRANS_TYPE_HK);
                tlBillTransCompareDetail.setCompareStatus(Constant.COMPARE_FLAG_0);
                tlBillTransCompareDetail.setCompareDesc("该转账明细金额或状态不平");
                tlBillTransCompareDetail.setCustomerId(item.getCustomerId());
                tlBillTransCompareDetail.setLoanNumber(tlBillTransDetil.getLoanNumber());
                tlBillTransCompareDetail.setSeqNo(item.getRefNo());
                tlBillTransCompareDetail.setDetailAmount(tlBillTransDetil.getTransactionAmount());
                tlBillTransCompareDetail.setTxnPostingAmnt(item.getAmount());
                tlBillTransCompareDetail.setBillTransStatus(tlBillTransDetil.getTransStatus());
                tlBillTransCompareDetail.setTxnTransStatus(item.getSumStatus());
                tlBillTransCompareDetail.setDataFrom(billCompareConfig.getDataFrom());
                tlBillTransCompareDetail.setBusinessDate(tlBillTransDetil.getBusinessDate());
                tlBillTransCompareDetailList.add(tlBillTransCompareDetail);
            }

            tlBillTransDetil.setCompareFlag(Constant.COMPARE_FLAG_1);
            tlBillTransDetil.setDataFrom(billCompareConfig.getDataFrom());
            tlBillTransDetilList.add(tlBillTransDetil);
        }

        BillDetilReadBean billDetilReadBean = new BillDetilReadBean();
        billDetilReadBean.setTlBillTransDetilList(tlBillTransDetilList);
        billDetilReadBean.setTlBillTransCompareDetailList(tlBillTransCompareDetailList);
        return billDetilReadBean;
    }
}
